﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Designtime.Metadata;
using CPPEI.Coolzon.UI.Views;
using CPPEI.Coolzon.UI.XProperty;
using CPPEI.Coolzon.UI.XProperty.Converters;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Designer.Views
{
    public class OpenSolutionThread
    {
        private static Guid _SolutionID;
        private static Solution _Solution;
        private static int _OpenTag;
        public static void Start(Guid solutionID, int openTag = 0)
        {
            if (_SolutionID != Guid.Empty)
            {
                return;
            }

            Event.UI.UIEventBus.Publish(new Event.UI.UICloseSolutionEvent());

            _SolutionID = solutionID;
            _OpenTag = openTag;

            BackgroundWorker bgw = new BackgroundWorker();
            bgw.DoWork += Bgw_DoWork;
            bgw.ProgressChanged += Bgw_ProgressChanged;
            bgw.RunWorkerCompleted += Bgw_RunWorkerCompleted;
            bgw.WorkerReportsProgress = true;
            bgw.RunWorkerAsync();
        }
        private static void Bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if ((bool)e.Result)
            {
                Event.UI.UIEventBus.Publish(new Event.UI.UIOpenSolutionEvent((bool)e.Result, _Solution,_OpenTag));
            }
            else
            {
                Event.UI.UIEventBus.Publish(new Event.UI.UIOpenSolutionEvent((bool)e.Result, _Solution, "方案不存在"));
                Logger.ErrorToTag(Library.LogTAG, "打开方案失败,方案不存在");
            }
            _SolutionID = Guid.Empty;
        }

        private static void Bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

        }

        private static void Bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                _Solution = LocalStorage.LoadSolution(UIContext.Current.LocalWorkSpace.LocalPath, _SolutionID);
                if (_Solution == null)
                {
                    e.Result = false;
                    return;
                }

                SolutionTreeViewNode tvNode = SolutionTreeViewNodeConverter.Convert(_Solution, UpdateStatus);
                UIContext.Current.SolutionNode = tvNode;

                ConvertContext convertContext = new ConvertContext();
                convertContext.SolutionName = _Solution.Name;
                convertContext.SolutionDirectory = UIContext.Current.LocalWorkSpace.LocalPath + _Solution.ID + "\\";
                convertContext.SolutionFilePath = UIContext.Current.LocalWorkSpace.LocalPath + _Solution.ID + "\\" + _Solution.ID + ".xml";
                PartInstanceCache.SetConvertContext(convertContext);
                XPropertyRTUtil.SetConvertContext(convertContext);
                // 初始化方案资源管理器
                Coolzon.UI.SolutionResourceManager.Init(convertContext.SolutionDirectory);

                e.Result = true;
            }
            catch (Exception ex)
            {
                e.Result = false;
                Logger.ErrorToTag(Library.LogTAG, "打开方案失败,{0}", ex);
            }
        }

        public static void UpdateStatus(SolutionTreeViewNode node)
        {
            VSFile vsFile = VSFileMgr.GetFileWithDefault(UIContext.Current.LocalWorkSpace.LocalPath, _SolutionID, node.ID);
            if (vsFile != null)
            {
                if (vsFile.Status == VSFileStatus.IsEditing)
                {
                    switch (vsFile.Action)
                    {
                        case VSFileAction.None:
                        case VSFileAction.Move:
                        case VSFileAction.Edit:
                            node.NodeStatus = NodeStatus.CheckOut;
                            break;
                        case VSFileAction.Add:
                            node.NodeStatus = NodeStatus.Add;
                            break;
                        case VSFileAction.Delete:
                            node.NodeStatus = NodeStatus.Delete;
                            break;
                    }
                }
                else if (vsFile.Status == VSFileStatus.IsLocking)
                {
                    node.NodeStatus = NodeStatus.OthersLocked;
                }
                else
                {
                    node.NodeStatus = NodeStatus.CheckIn;
                }
            }
            else
            {
                node.NodeStatus = NodeStatus.NotInSourceControl;
            }
        }
    }
}
